<?php
namespace Home\Controller\Mobile;

class NotifyController{
	
	public function notify_url(){
		$out_trade_no = I('request.out_trade_no');
		$order_num = I('request.order_num');
		$status = I('request.status');
		
		$order = M('sxkj_offline_order')->where(array('order_num'=>$order_num))->find();	
		
		$old_status = $order['status'];
		if($status==1){
            $true_status = $old_status+1;
        }else{
            if($old_status==0){
                $true_status = -2;
            }else{
                $true_status = $old_status;
            }
        }

		$data['out_trade_no'] = $out_trade_no;
        $data['status'] = $true_status;
        $data['update_time'] = time();
        $res = M('sxkj_offline_order')->where(array('id'=>$order['id']))->data($data)->save();
		
		if($res){
            $commission = json_decode($order['commission'],true);

            //审核记录
            $edata['type'] = 1;
            $edata['uid'] = $order['uid'];
            $edata['phone'] = $order['phone'];
            $edata['orderid'] = $order['id'];
            $edata['reason'] = I('request.reason');
            $edata['status'] = $status;
            if($status==1){
                $edata['true_status'] = $true_status;
                $edata['commission'] = $commission[$old_status];
                
                //审核通过加佣金
                $user_info = M('sxkj_offline_user')->field('sup_promoterid,cash')->where('id=%d',$order['uid'])->find();
                $data_user['cash'] = $user_info['cash']+$commission[$old_status];
				M('sxkj_offline_user')->where('id=%d',$order['uid'])->save($data_user);
				
				//分销
				$sup_promoterid = $user_info['sup_promoterid'];
	           	if(!empty($sup_promoterid)){
	           		$par['flag'] = 'system';
					$system = M('sxkj_offline_data')->where($par)->find();
					$system = json_decode($system['data'],true);
	           		$one_fx = $system['one_fx'];
	           		$fecommission = $commission[$old_status]*$one_fx/100;
	           		$fecommission = round($fecommission,2);
	           		
	           		$fedata['type'] = 2;
		            $fedata['uid'] = $sup_promoterid;
		            $fedata['phone'] = $order['phone'];
		            $fedata['orderid'] = $order['id'];
		            $fedata['status'] = 1;
	                $fedata['true_status'] = $true_status;
	                $fedata['commission'] = $fecommission;
		            $fedata['add_time'] = time();
		            M('sxkj_offline_examine')->data($fedata)->add();
		            
		           	//加分销佣金
	                $user_info = M('sxkj_offline_user')->field('cash')->where('id=%d',$sup_promoterid)->find();
	                $data_user['cash'] = $user_info['cash']+$fecommission;
					M('sxkj_offline_user')->where('id=%d',$sup_promoterid)->save($data_user); 
	           	}
            }
            $edata['add_time'] = time();
            M('sxkj_offline_examine')->data($edata)->add();
           
            return 'success';
        }else{
            return 'error';
        }
	}
 	
 	
}